package com.ctyun.xstore.vod.internal;

import java.lang.reflect.Type;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.ctyun.xstore.vod.template.model.TemplateEnums.TemplateFormatEnum;
import com.ctyun.xstore.vod.transcode.model.TranscodeTaskStatusEnum;
import com.ctyun.xstore.vod.video.model.VideoEnums.VideoAuditEnum;
import com.ctyun.xstore.vod.video.model.VideoEnums.VideoFileTypeEnum;
import com.ctyun.xstore.vod.video.model.VideoEnums.VideoUploadEnum;
import com.ctyun.xstore.vod.vodsetting.model.SettingEnums.ACL;
import com.ctyun.xstore.vod.vodsetting.model.SettingEnums.RefererType;

public class FastJsonConfig {

	private static class EnumsToStringSerializer implements ObjectSerializer {
		@Override
		public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
				int features) {
			serializer.write(object.toString());
		}
	}

	private static class EnumStringDeserializer implements ObjectDeserializer {

		@SuppressWarnings("unchecked")
		@Override
		public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
			if (type.equals(ACL.class)) {
				try {
					String str = parser.parseObject(String.class).toUpperCase().replace("-", "_");
					return (T) ACL.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			} else if (type.equals(RefererType.class)) {
				try {
					String str = parser.parseObject(String.class).toUpperCase().replace("-", "_");
					return (T) RefererType.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			} else if (type.equals(VideoUploadEnum.class)) {
				try {
					String str = parser.parseObject(String.class).toUpperCase();
					return (T) VideoUploadEnum.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			} else if (type.equals(VideoFileTypeEnum.class)) {
				try {
					String str = parser.parseObject(String.class).toUpperCase();
					return (T) VideoFileTypeEnum.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			} else if (type.equals(VideoAuditEnum.class)) {
				try {
					String str = parser.parseObject(String.class).toUpperCase();
					return (T) VideoAuditEnum.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			} else if(type.equals(TranscodeTaskStatusEnum.class)) {
				try {
					String str = parser.parseObject(String.class);
					return (T) TranscodeTaskStatusEnum.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			} else if(type.equals(TemplateFormatEnum.class)) {
				try {
					String str = parser.parseObject(String.class).toUpperCase();
					return (T) TemplateFormatEnum.valueOf(str);
				} catch (Exception e) {
					// ignore this exception
				}
			}
			return null;
		}

		@Override
		public int getFastMatchToken() {
			return 0;
		}

	}

	private static volatile SerializeConfig serializeConfig;

	private static volatile ParserConfig parserConfig;

	public static SerializeConfig getSerializeConfig() {
		if (serializeConfig == null) {
			synchronized (FastJsonConfig.class) {
				if (serializeConfig == null) {
					SerializeConfig config = new SerializeConfig();
					EnumsToStringSerializer serializer = new EnumsToStringSerializer();
					config.put(ACL.class, serializer);
					config.put(RefererType.class, serializer);
					config.put(VideoUploadEnum.class, serializer);
					config.put(VideoFileTypeEnum.class, serializer);
					config.put(VideoAuditEnum.class, serializer);
					config.put(TranscodeTaskStatusEnum.class, serializer);
					config.put(TemplateFormatEnum.class, serializer);
					serializeConfig = config;
				}
			}
		}
		return serializeConfig;
	}

	public static ParserConfig getParseConfig() {
		if (parserConfig == null) {
			synchronized (FastJsonConfig.class) {
				if (parserConfig == null) {
					ParserConfig config = new ParserConfig();
					EnumStringDeserializer deserializer = new EnumStringDeserializer();
					config.putDeserializer(ACL.class, deserializer);
					config.putDeserializer(RefererType.class, deserializer);
					config.putDeserializer(VideoUploadEnum.class, deserializer);
					config.putDeserializer(VideoFileTypeEnum.class, deserializer);
					config.putDeserializer(VideoAuditEnum.class, deserializer);
					config.putDeserializer(TranscodeTaskStatusEnum.class, deserializer);
					config.putDeserializer(TemplateFormatEnum.class, deserializer);
					parserConfig = config;
				}
			}
		}
		return parserConfig;
	}

}
